package com.zhoudy.segmetqueue;

import com.zhoudy.segmetlock.SegmentDataWithLock;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.locks.Lock;


public class TransferDat {

    private SegmentDataWithQueue segmentDataWithQueue;

    private List<String> newDataContainer = new ArrayList<>();

    public TransferDat() {
        segmentDataWithQueue = new SegmentDataWithQueue();
    }

    /**
     * 切分数据
     *
     * @param data
     */
    public void segmentData(String[] data) {
        segmentDataWithQueue.blockFullWithSegmentation(data);
    }

    /**
     * 迁移数据
     */
    public void transfer() {
        try {
            while (!segmentDataWithQueue.isEmptyQueue()) {
                List<String> dataNodeList = segmentDataWithQueue.getNodeFromDataQueue();

                if (!CollectionUtils.isEmpty(dataNodeList)) {
                    System.out.println(System.currentTimeMillis() + ": " + Thread.currentThread().getName() +
                            ": transfer data :" + dataNodeList.toString());
                    newDataContainer.addAll(dataNodeList);
                    Thread.sleep(300L);
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(System.currentTimeMillis() + ": " + Thread.currentThread().getName() + ": transfer complete ");
    }


    public boolean isFinish() {
        return segmentDataWithQueue.isEmptyQueue();
    }

    public List<String> getNewDataContainer() {
        return newDataContainer;
    }

}


